package cj.web.admin.controllers;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import cj.utils.Constants;
import cj.web.admin.dao.AreaDAO;
import cj.web.admin.domain.Area;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/area")
public class AreaController extends BaseController{

	private static Logger logger = Logger.getLogger(AreaController.class.getName());
	
	@Autowired
	private AreaDAO areaDao;
	
	@Get("show")
	public String show(Model model,Area area) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.qygl);
		return "/views/game/areaList.vm";
	}

	@Post("getList")
	public String queryList(Area area) {
	
		JSONObject jo = new JSONObject();
		
		List<Area> objs = areaDao.query(area);
		
		int size = areaDao.querysize(area);
		jo.put("data", objs);
		jo.put("draw", area.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.qygl);
		return "/views/game/areaDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Area area,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			area.setCreateUser(returnSessionObj(request).getAccount());
			area.setUpdateUser(returnSessionObj(request).getAccount());
			areaDao.create(area);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Area obj = areaDao.queryById(id);
		model.add("obj", obj);
		//MemuEnum
        model.add("MemuEnum",MemuEnum.qygl);
		return "/views/game/areaEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Area area,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			area.setUpdateUser(returnSessionObj(request).getAccount());
			areaDao.update(area);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		areaDao.deleteById(id);
		return "r:show";
	}
}
